import type {RecordType} from "plus-pro-components/lib/types";

export interface TransferRecord extends RecordType {
    transaction_counterparty?: string
    transaction_time?: Date
    transaction_type?: string
    product?: string
    income_or_expense?: string
    amount?: number
    payment_method?: string
    current_status?: string
    transaction_id?: string
    merchant_id?: string
    notes?: string
    archived?: boolean
}

/**
 * 比较两个值是否相等（支持基本类型、Date、对象、数组）
 */
function isEqual(a: any, b: any): boolean {
    // 处理 null/undefined
    if (a === null || b === null) {
        return a === b;
    }
    if (a === undefined || b === undefined) {
        return a === b;
    }

    // 处理 Date 类型（比较时间戳）
    if (a instanceof Date && b instanceof Date) {
        return a.getTime() === b.getTime();
    }

    // 处理基本类型（非对象）
    if (typeof a !== 'object' || typeof b !== 'object') {
        return a === b;
    }

    // 处理数组
    if (Array.isArray(a) && Array.isArray(b)) {
        if (a.length !== b.length) return false;
        return a.every((item, index) => isEqual(item, b[index]));
    }

    // 处理对象（比较所有键）
    const keysA = Object.keys(a);
    const keysB = Object.keys(b);
    if (keysA.length !== keysB.length) return false;

    return keysA.every(key => {
        if (!keysB.includes(key)) return false; // 键名必须一致
        return isEqual(a[key], b[key]); // 递归比较键值
    });
}

// 比较两个 TransferRecord 对象
export function areTransferRecordsEqual(a: TransferRecord, b: TransferRecord): boolean {
    return isEqual(a, b);
}